package com.zjm.find.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zjm.find.pojo.*;
import com.zjm.find.service.UserService;
import com.zjm.find.utils.JSONResult;
import com.zjm.find.utils.TimeUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "登录", notes = "微信登录")
    @PostMapping("/login")
    public JSONResult login(String code, String name, int sex, String avatar) throws ParseException {
        //性别判断
        String gender = sex == 1 ? "男" : "女";
        //微信官方验证
        WXSessionModel model = userService.wxAuthorization(code);
        User user = new User();
        user.setName(name);
        user.setSex(gender);
        user.setAvatar(avatar);
        user.setOpenId(model.getOpenid());
        user.setSessionKey(model.getSession_key());
        //是否为新用户
        if (!StringUtils.isEmpty(userService.checkUserExistByOpenId(user))) {
            //老用户，更新信息
            user.setUpdateTime(TimeUtils.getStringTime());
            userService.updateUserInfoByOpenId(user);
        } else {
            //新用户，插入信息
            user.setCreateTime(TimeUtils.getStringTime());
            userService.addUser(user);
        }
        return JSONResult.msgAndData("登陆成功", userService.checkUserExistByOpenId(user));
    }

    @PostMapping("createSuggestion")
    public JSONResult CreateSuggestion(Suggestion suggestion) throws ParseException {
        suggestion.setCreateTime(TimeUtils.getStringTime());
        if (userService.createSuggestion(suggestion)) {
            return JSONResult.successMsg("反馈成功");
        }
        return JSONResult.errorMsg("反馈失败");
    }

    @PostMapping("/createFollow")
    public JSONResult createFollow(UserFollow userFollow) throws ParseException {
        userFollow.setCreateTime(TimeUtils.getDateTime());
        if (userService.createFollow(userFollow)) {
            return JSONResult.successMsg("关注成功");
        } else {
            return JSONResult.errorMsg("关注失败");
        }
    }

    @PostMapping("/cancelFollow")
    public JSONResult cancelFollow(UserFollow userFollow) {
        if (userService.cancelFollow(userFollow)) {
            return JSONResult.successMsg("取消关注成功");
        } else {
            return JSONResult.errorMsg("取消关注失败");
        }
    }

    @GetMapping("/selectFollow")
    public JSONResult selectFollow(UserFollow userFollow,
                                   @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                   @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit) {
        PageHelper.startPage(page, limit);
        PageInfo<Moment> followPageInfo = new PageInfo(userService.selectFollow(userFollow));
        return JSONResult.msgAndData("查询成功", followPageInfo);
    }


    @GetMapping("/selectUserFollow")
    public JSONResult selectUserFollow(UserFollow userFollow,
                                       @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                       @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit) {
        PageHelper.startPage(page, limit);
        PageInfo<Moment> followPageInfo = new PageInfo(userService.selectUserFollow(userFollow));
        return JSONResult.msgAndData("查询成功", followPageInfo);
    }

    @PostMapping("/createHistory")
    public JSONResult createHistory(UserHistory userHistory) throws ParseException {
        userHistory.setCreateTime(TimeUtils.getDateTime());
        if (userService.createHistory(userHistory)) {
            return JSONResult.successMsg("创建历史记录成功");
        } else {
            return JSONResult.errorMsg("创建历史记录失败");
        }
    }

    @PostMapping("/deleteHistory")
    public JSONResult deleteHistory(UserHistory userHistory) {
        if (userService.deleteHistory(userHistory)) {
            return JSONResult.successMsg("删除历史成功");
        } else {
            return JSONResult.errorMsg("删除历史失败");
        }
    }

    @GetMapping("/allUser")
    public JSONResult allUser(User user,
                              @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                              @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit) {
            PageHelper.startPage(page, limit);
            PageInfo list = new PageInfo(userService.allUser(user));
            return JSONResult.msgAndData("查询成功", list);
    }
}
